import { BufferGeometry, Mesh, Object3D } from 'three';
import { Command } from '../command.js';
import { Editor } from '../editor.js';

/**
 * @param editor Editor
 * @param object THREE.Object3D
 * @param newGeometry THREE.Geometry
 * @constructor
 */

class SetGeometryCommand extends Command {
	private object:Mesh;
	private oldGeometry:BufferGeometry|null;
	private newGeometry:BufferGeometry;
	constructor( editor:Editor, object:Object3D, newGeometry:BufferGeometry ) {

		super( editor );

		this.type = 'SetGeometryCommand';
		this.name = 'Set Geometry';
		this.updatable = true;

		this.object = object as Mesh;
		this.oldGeometry = ( object !== undefined ) ? this.object.geometry : null;
		this.newGeometry = newGeometry;

	}

	execute() {

		this.object.geometry.dispose();
		this.object.geometry = this.newGeometry;
		this.object.geometry.computeBoundingSphere();

		this.editor.signals.geometryChanged.dispatch( this.object );
		this.editor.signals.sceneGraphChanged.dispatch();

	}

	undo() {

		this.object.geometry.dispose();
		if(this.oldGeometry)
		this.object.geometry = this.oldGeometry;
		this.object.geometry.computeBoundingSphere();

		this.editor.signals.geometryChanged.dispatch( this.object );
		this.editor.signals.sceneGraphChanged.dispatch();

	}

	update( cmd:SetGeometryCommand ) {

		this.newGeometry = cmd.newGeometry;

	}


}

export { SetGeometryCommand };
